package com.jz.digital.utils;

import com.jz.digital.bean.UserInfo;
import com.jz.digital.bean.DaoSession;
import com.jz.digital.bean.UserInfoDao;
import com.jz.digital.database.GreenDaoManager;

/**
 * ================================================
 * Created: WangYong
 * Date: 2024/11/25
 * Description:
 * ================================================
 */
public class GreenDaoUtils {
    private final static String TAG = GreenDaoUtils.class.getSimpleName();

    public static DaoSession getDaoSession() {
        return GreenDaoManager.getInstance().getDaoSession();
    }

    public static void saveUserInfo(UserInfo userInfo) {
        if (userInfo == null) return;
        UserInfo localUserInfo = getUserInfo(userInfo.getUserId());
        if (localUserInfo != null) {
            userInfo.setId(localUserInfo.getId());
            getDaoSession().getUserInfoDao().update(userInfo);
        } else {
            getDaoSession().getUserInfoDao().insert(userInfo);
        }
    }

    /**
     * 查询本地userInfo
     */
    public static UserInfo getUserInfo(String userId) {
        long count = getDaoSession().getUserInfoDao().queryBuilder()
                .where(UserInfoDao.Properties.UserId.eq(userId))
                .count();
        if (count <= 0) return null;
        UserInfo userInfo = getDaoSession().getUserInfoDao().queryBuilder()
                .where(UserInfoDao.Properties.UserId.eq(userId))
                .limit(1).unique();
        if (userInfo == null) return null;
        return getDaoSession().getUserInfoDao().queryBuilder()
                .where(UserInfoDao.Properties.UserId.eq(userId))
                .limit(1).unique();
    }
}
